package com.scut.edu.wanyou;

import com.scut.edu.wanyou.R;
import com.scut.edu.wanyou.conf.Config;
import com.weibo.sdk.android.Oauth2AccessToken;
import com.weibo.sdk.android.keep.AccessTokenKeeper;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-4-4
 * Time: 下午6:16
 * To change this template use File | Settings | File Templates.
 */
public class InitActivity extends Activity {

    private LinearLayout account_login = null;
    private LinearLayout account_register = null;
    
    private Button sina_login = null;
    
    private Oauth2AccessToken accessToken = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
    	requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_init);
        
        account_login = (LinearLayout)findViewById(R.id.account_login);
        account_login.setOnClickListener(listener);
        
        account_register = (LinearLayout)findViewById(R.id.account_register);
        account_register.setOnClickListener(listener);
        
        sina_login = (Button)findViewById(R.id.sina_login);
        sina_login.setOnClickListener(listener);
    }

    /**登录、注册按钮监听器
     *
     */
    private Button.OnClickListener listener = new Button.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
        	Intent intent = null;
            switch (v.getId()) {
                case R.id.account_login:
                	intent = new Intent(InitActivity.this,LoginActivity.class);
                	startActivity(intent);
                	finish();
                	break;
                case R.id.account_register:
                	intent = new Intent(InitActivity.this,RegisterActivity.class);
                	startActivity(intent);
                	finish();
                	break;
                case R.id.sina_login:
                	intent = new Intent(InitActivity.this,SinaOAuthActivity.class);
                	startActivityForResult(intent, Config.SINA_OAUTH_REQUEST_CODE);
                	break;
                default:
                    break;
            }
            
        }
    };

    /**注册
     * @param email
     * @param password
     * @return
     */
    private boolean doRegister(String email,String password){
        return true;
    }

    /**登录
     * @param email
     * @param password
     * @return
     */
    private boolean doLogin(String email,String password){

        return true;
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        Log.i("LoginActivity", "暂停");
        super.onPause();
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        Log.i("LoginActivity", "停止");
        this.onDestroy();
        super.onStop();
    }

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if(data == null)
		{
			return;
		}
		if(resultCode == SinaOAuthActivity.OAUTH_RESULT_CODE)
		{
			accessToken = (Oauth2AccessToken)data.getExtras().getSerializable("accessToken");
			
			if(accessToken!=null&&accessToken.isSessionValid())
			{
				AccessTokenKeeper.keepAccessToken(getApplicationContext(), accessToken);
				Toast.makeText(getApplicationContext(), "认证成功！", Toast.LENGTH_LONG).show();
				Intent intent = new Intent(InitActivity.this,MainActivity.class);
				startActivity(intent);
				InitActivity.this.finish();
			}else {
				Toast.makeText(getApplicationContext(), "认证失败！", Toast.LENGTH_LONG).show();
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
		
	}
    
    
}